<?php
	include("includes/secure.php");
	include("includes/config.php");
	include("includes/class/case.class.php");
	include("includes/class/payment.class.php");
	include("includes/class/client.class.php");
	include("includes/class/user.class.php");
	
	$myCase = new MyCase();
	$payment = new Payment();
	$client = new Client();
	$user = new User();
	
	$case_code = 0;
	$bus_code = 0;
	$use_code = 0;
	$cli_code = 0;
	$case_number = 0;
	$cli_namee = 0;
	$cli_lastname = 0;
	$case_comment = '';
	$case_balance = 0;
	$case_approved = 0;
	$countPaymentDetail = 0;	
		
	$msg_info = $msg_alert = $msg_error = false;
	$_SESSION['msg_cool'] = '';
	$show_msg = 0;
	
	$listBusinessType = $myCase->listBusinessType();
	
	$getNumber = $myCase->getNumberCase();
	
	if ( $getNumber == 0)
	  $getNumber = 1;

	$getNumber= $getNumber+1;
	$cli_code = 0;
	$case_number = cerosIzquierda($getNumber);	

	if ( isset( $_GET["cli_code"] ) && (int) $_GET["cli_code"] > 0){
		$cli_code = (int) $_GET["cli_code"];
		$dataClient = $client->getClientData($cli_code);		
		$cli_name = $dataClient->cli_name;
		$cli_lastname = $dataClient->cli_lastname;		
	}
	if ( isset( $_GET["case_code"] ) && (int) $_GET["case_code"] > 0){
		$case_code = $_GET["case_code"];
		$dataCase = $myCase->getCase($case_code);
		
		$cli_code = $dataCase->cli_code;
		$case_number = $dataCase->case_number;
		$cli_name = $dataCase->cli_name;
		$cli_lastname = $dataCase->cli_lastname;
		$bus_code = $dataCase->bus_code;
		$use_code = $dataCase->use_code;
		$case_comment = $dataCase->case_comment;
		$case_balance = $dataCase->case_balance;
		$case_approved = $dataCase->case_approved;
		
		$countPaymentDetail = $payment->countPaymentDetail($case_code);		
	}
	else
		$listClients = $myCase->listClients();
		
	$listAdvisers = $user->listAdvisers();
	
	if ( !$case_number )
		   $case_number = 1;
	
	if ( isset ($_POST['add']) && $_POST['add'] == 1){
		
		
		$action = $_POST["action"];
		$data->case_code = $_POST["case_code"];
		
		$data->cli_code = trim(mysql_real_escape_string($_POST['cli_code']));
		$data->use_code = $_POST['use_code'];
		$data->bus_code = trim(mysql_real_escape_string($_POST['bus_code']));
		$data->case_number = $_POST['case_number'];
		$data->case_comment = trim(mysql_real_escape_string($_POST['case_comment']));
		$data->case_balance = (float) $_POST['case_balance'];
		$data->case_approved = (int) $_POST['case_approved'];
		
		if( $case_code ){
			if ( $myCase->updateCase($data) ){
				$msg_info = true;
				$_SESSION["msg_cool"] .= 'The case was updated successfully.<br />';
			}
			else{
				$msg_error = true;
				$_SESSION["msg_cool"] .= 'It could not be updated the case.<br />';
				echo "<script> window.location.href='listClient.php?show_msg=1'</script>";
			}
		}
		elseif ( $data->case_code = $myCase->addCase($data) ){
			$msg_info = true;
			$_SESSION["msg_cool"] .= 'The case was added successfully.<br />';
		}
		else{
			$msg_info = true;
			$_SESSION["msg_cool"] .= 'It could not be added the case.<br />';
			echo "<script> window.location.href='listClient.php?show_msg=1'</script>";
		}
		
		if ( $action == 1 )
			echo "<script> window.location.href='listBusiness.php?show_msg=3'</script>";
		elseif ( $action == 2 )
			echo "<script> window.location.href='addRequest.php?case_code=".$data->case_code."'</script>";
		
	}
	
	if ( $msg_error )
		$show_msg = 1;
	elseif ( $msg_alert )
		$show_msg = 2;
	elseif ( $msg_info )
		$show_msg = 3;
?>